package com.example.service.impl;

import com.example.domain.entity.Dept;
import com.example.mapper.DeptMapper;
import com.example.service.IDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.domain.query.DeptQuery;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-05-15
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Override
    public IPage<Dept> pageQuery(DeptQuery query, int pageNum, int pageSize) {
        Page<Dept> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        if (query != null) {
            if (query.getId() != null) {
                wrapper.eq("id", query.getId());
            }
            if (StringUtils.hasText(query.getDeptName())) {
                wrapper.like("dept_name", query.getDeptName());
            }
            if (StringUtils.hasText(query.getManager())) {
                wrapper.like("manager", query.getManager());
            }
            if (StringUtils.hasText(query.getPhone())) {
                wrapper.like("phone", query.getPhone());
            }
            if (StringUtils.hasText(query.getPlaceBuilding())) {
                wrapper.like("place_building", query.getPlaceBuilding());
            }
            if (StringUtils.hasText(query.getPlaceRoom())) {
                wrapper.like("place_room", query.getPlaceRoom());
            }
            if (StringUtils.hasText(query.getDeptDuty())) {
                wrapper.like("dept_duty", query.getDeptDuty());
            }
            if (query.getIsDirect() != null) {
                wrapper.eq("is_direct", query.getIsDirect());
            }
        }
        return page(page, wrapper);
    }
}
